package cn.yunzhisheng.vui.assistant.tv.view;

import cn.yunzhisheng.vui.assistant.tv.R;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GlobalVoiceView extends LinearLayout {
    private static final String TAG = "GlobalVoiceView";
    private ImageView mVoiceBg;
    private ImageView mVoiceLevel;
    private ImageView mVoiceRing;
    private TextView mStateText;

    private ImageView mRatoteImage;

    private Animation operatingAnim;
    private LinearInterpolator lin;

    private Animation mRecognitionScaleAnim;

    public GlobalVoiceView(Context context) {
        this(context, null);
    }

    public GlobalVoiceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void updateView(VoiceMode mode) {
        Log.d(TAG, "global record sound update view " + mode);
        switch (mode) {
        case MODE_RECORDING:
            setVisibility(View.VISIBLE);
            mStateText.setVisibility(View.VISIBLE);
            mVoiceRing.setVisibility(View.VISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default_current);
            mStateText.setText(R.string.voice_release);
            break;
        case MODE_RECOGNISING:
            setVisibility(View.VISIBLE);
            mStateText.setVisibility(View.VISIBLE);
            // mProgress.setVisibility(View.VISIBLE);
            // mVoiceBg.setImageResource(R.drawable.ic_voice_default);

            mRatoteImage.setVisibility(View.VISIBLE);
            mRatoteImage.startAnimation(operatingAnim);

            mVoiceBg.setImageResource(R.drawable.ic_voice_prasing);
            mVoiceRing.setVisibility(View.GONE);

            mStateText.setText(R.string.voice_recognising);
            mVoiceLevel.setVisibility(View.GONE);
            break;
        case MODE_UNRECOGNISED:
            setVisibility(View.VISIBLE);
            mStateText.setVisibility(View.VISIBLE);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default);
            setRatoteImageGone();

            mStateText.setText(R.string.voice_unrecognised);
            mVoiceLevel.setVisibility(View.GONE);
            break;
        case MODE_NO_VOICE:
            setVisibility(View.VISIBLE);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default);

            mStateText.setVisibility(View.VISIBLE);
            mStateText.setText(R.string.voice_no);
            // setVoiceLevel(0);
            mVoiceLevel.setVisibility(View.GONE);
            break;
        case MODE_RECORDINGINIT:
            setVisibility(View.VISIBLE);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default);

            mStateText.setVisibility(View.VISIBLE);
            mStateText.setText(R.string.voice_recording_init);
            mVoiceLevel.setVisibility(View.GONE);
            break;
        case MODE_UNAVILIABLE:
            setEnabled(false);
            break;
        case MODE_DEFAULT:
            setEnabled(true);
            mStateText.setVisibility(View.VISIBLE);
            // mStateText.setText(R.string.voiceshowtext);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default_current);

            setRatoteImageGone();
            mVoiceLevel.setVisibility(View.GONE);
            break;
        }
    }

    private void setRatoteImageGone() {
        mRatoteImage.clearAnimation();
        mRatoteImage.setVisibility(View.GONE);
    }

    public void updateVolume(int volume) {
        // LogUtil.d(TAG, "volume:" + volume);

        if (volume > 10) {
            mVoiceBg.setImageResource(R.drawable.ic_voice_speaking);
            mVoiceLevel.setVisibility(View.VISIBLE);
            setRatoteImageGone();
        }

        if (volume < 10) {
            setVoiceLevel(1);
        } else if (volume < 20) {
            setVoiceLevel(2);
        } else if (volume < 30) {
            setVoiceLevel(3);
        } else if (volume < 40) {
            setVoiceLevel(4);
        } else if (volume < 50) {
            setVoiceLevel(5);
        } else if (volume < 60) {
            setVoiceLevel(6);
        } else if (volume < 70) {
            setVoiceLevel(7);
        } else if (volume < 80) {
            setVoiceLevel(8);
        } else if (volume < 90) {
            setVoiceLevel(9);
        } else {
            setVoiceLevel(10);
        }
    }

    public void setVoiceLevel(int level) {
        mVoiceLevel.getDrawable().setLevel(level);
    }

    private void init() {
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.global_voice, this);
        mVoiceBg = (ImageView) view.findViewById(R.id.voice_bg);
        mVoiceLevel = (ImageView) view.findViewById(R.id.voice_level);
        mVoiceRing = (ImageView) view.findViewById(R.id.voice_ring);
        mStateText = (TextView) view.findViewById(R.id.state_text);

        mRatoteImage = (ImageView) view.findViewById(R.id.rotate_animation);

        lin = new LinearInterpolator();
        operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_sacnner);
        operatingAnim.setInterpolator(lin);

        /** ---Dialog 进入Scale动画---modify by WLP at 2013-12-04--- */
        mRecognitionScaleAnim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_scale_in);
        view.startAnimation(mRecognitionScaleAnim);
        /** ---Dialog 进入Scale动画---modify by WLP at 2013-12-04--- */
    }

}
